﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAA.Forms.Components
{
    public partial class ButtonListPane : UserControl
    {
        private List<PictureBoxX> _lstButton;

        private event EventHandler _onButtonClick;

        public EventHandler OnButtonClick
        {
            get { return _onButtonClick; }
            set { _onButtonClick = value; }
        }

        private int _iHorizontalCount;

        public int HorizontalCount
        {
            get { return _iHorizontalCount; }
            set { _iHorizontalCount = value; }
        }

        private int _iVerticalCount;

        public int VerticalCount
        {
            get { return _iVerticalCount; }
            set { _iVerticalCount = value; }
        }

        private DirectionEnum _eDirection = DirectionEnum.Vertical;

        public DirectionEnum Direction
        {
            get { return _eDirection; }
            set { _eDirection = value; }
        }

        private int _iCurrentPage;

        public int CurrentPage
        {
            get { return _iCurrentPage; }
            set 
            { 
                _iCurrentPage = value;
                Reposition();
            }
        }

        private int _iTotalPages;

        public int TotalPages
        {
            get { return _iTotalPages; }
        }

        private int _iXPadding;

        public int XPadding
        {
            get { return _iXPadding; }
            set { _iXPadding = value; }
        }

        private int _iYPadding;

        public int YPadding
        {
            get { return _iYPadding; }
            set { _iYPadding = value; }
        }

        private int _iCurrentXPadding;
        private int _iCurrentYPadding;
        private int _iMaxButtonWidth;
        private int _iMaxButtonHeight;

        private AdjustPaddingEnum _eAdjustPadding;

        public AdjustPaddingEnum AdjustPadding
        {
            get { return _eAdjustPadding; }
            set { _eAdjustPadding = value; }
        }

        public ButtonListPane()
        {
            InitializeComponent();
            _lstButton = new List<PictureBoxX>();
        }

        private void DefaultButtonClick(object sender, EventArgs e)
        {
            if(OnButtonClick != null)
                OnButtonClick(sender, e);
        }

        private bool _isDisplayPageHandler;

        public bool IsDisplayPageHandler
        {
            get { return _isDisplayPageHandler; }
            set 
            { 
                _isDisplayPageHandler = value;
                pnlNavigator.Visible = value;
                Reposition();
            }
        }

        public void Clear()
        {
            _lstButton = new List<PictureBoxX>();
        }

        public void AddButton(string strText, string strNormalImage)
        {
            AddButton(strText, strNormalImage, null);
        }

        public void AddButton(string strText, string strNormalImage, EventHandler onButtonClick)
        {
            AddButton(strText, strNormalImage, null, onButtonClick);
        }

        public void AddButton(string strText, string strNormalImag, string strCommand, EventHandler onButtonClick)
        {
            AddButton(strText, strNormalImag, strNormalImag, strCommand, onButtonClick);
        }

        public void AddButton(string strText, string strNormalImage, string strSelectedImage, string strCommand, EventHandler onButtonClick)
        {
            PictureBoxX btnButton = new PictureBoxX();
            if (strNormalImage != null)
                btnButton.LoadImage(strNormalImage, strNormalImage, strNormalImage, strSelectedImage);
            if (strText != null)
                btnButton.Text = strText;
            if (strCommand != null)
                btnButton.Tag = strCommand;
            if(onButtonClick != null)
                btnButton.Click += onButtonClick;

            _lstButton.Add(btnButton);
        }

        public void Reposition()
        {
            int iStartIndex = CurrentPage * HorizontalCount * VerticalCount;

            if (_iMaxButtonHeight == 0 || _iMaxButtonWidth == 0)
                CalculateDefaultVariable();

            while (pnlButtons.Controls.Count > 0)
                pnlButtons.Controls.RemoveAt(0);

            int iXPadding = 0;
            int iYPadding = 0;

            switch (AdjustPadding)
            {
                case AdjustPaddingEnum.None:
                    iXPadding = XPadding;
                    iYPadding = YPadding;
                    break;

                case AdjustPaddingEnum.XPadding:
                    iXPadding = Math.Max(0, _iCurrentXPadding);
                    iYPadding = YPadding;
                    break;

                case AdjustPaddingEnum.YPadding:
                    iXPadding = XPadding;
                    iYPadding = Math.Max(0, _iCurrentYPadding);
                    break;

                case AdjustPaddingEnum.Both:
                    iXPadding = Math.Max(0, _iCurrentXPadding);
                    iYPadding = Math.Max(0, _iCurrentYPadding);
                    break;
            }

            int iIndex = 0;
            for (int i = iStartIndex; i < Math.Min((CurrentPage + 1) * HorizontalCount * VerticalCount, _lstButton.Count); i++)
            {
                if(Direction == DirectionEnum.Horizontal)
                {
                    _lstButton[i].Left = iXPadding + (iIndex % HorizontalCount) * (iXPadding + _iMaxButtonWidth);
                    _lstButton[i].Top = iYPadding + (int)Math.Floor(i / (double)HorizontalCount) * (iYPadding + _iMaxButtonHeight);
                }
                else
                {
                    _lstButton[i].Left = iXPadding + (int)Math.Floor(iIndex / (double)VerticalCount) * (iXPadding + _iMaxButtonWidth);
                    _lstButton[i].Top = iYPadding + (i % VerticalCount) * (iYPadding + _iMaxButtonHeight);
                }
                pnlButtons.Controls.Add(_lstButton[i]);
                iIndex++;
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (CurrentPage > 0)
                CurrentPage--;
            Reposition();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (CurrentPage < TotalPages - 1)
                CurrentPage++;
            Reposition();
        }

        private void pnlButtons_SizeChanged(object sender, EventArgs e)
        {
            CalculateDefaultVariable();
        }

        private void CalculateDefaultVariable()
        {
            if ((_lstButton.Count == 0) || (HorizontalCount == 0) || (VerticalCount == 0) || AdjustPadding == (AdjustPaddingEnum.None))
                return;

            if (IsDisplayPageHandler)
            {
                pnlButtons.Top = 0;
                pnlButtons.Left = 0;
                pnlButtons.Width = Width;
                pnlButtons.Height = Height - pnlNavigator.Height - 2;

                pnlNavigator.Top = Height - pnlNavigator.Height;
                pnlNavigator.Left = 0;
                pnlNavigator.Width = Width;
                
            }
            else
            {
                pnlButtons.Top = 0;
                pnlButtons.Left = 0;
                pnlButtons.Width = Width;
                pnlButtons.Height = Height;
            }

            _iMaxButtonHeight = 0;
            _iMaxButtonWidth = 0;
            for (int i = 0; i < _lstButton.Count; i++)
            {
                _iMaxButtonHeight = Math.Max(_lstButton[i].Height, _iMaxButtonHeight);
                _iMaxButtonWidth = Math.Max(_lstButton[i].Width, _iMaxButtonWidth);
            }

            _iCurrentXPadding = (int)Math.Floor((Width - _iMaxButtonWidth * HorizontalCount) / (HorizontalCount + 1.0));
            _iCurrentYPadding = (int)Math.Floor((Height - _iMaxButtonHeight * VerticalCount) / (VerticalCount + 1.0));

            Reposition();
        }




    }
}
